/*
 * Copyright (c) 2024 HiSilicon Technologies CO., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "common_def.h"
#include "pinctrl.h"
#include "uart.h"
#include "soc_osal.h"
#include "app_init.h"
#include "securec.h"
#include "sle_errcode.h"
#include "sle_connection_manager.h"
#include "sle_device_discovery.h"
#include "sle_server_adv.h"
#include "sle_server.h"
#include "uart_rx_tx.h"

#include "hal_bsp_oled/hal_bsp_ssd1306.h"
#include "app_init.h"
#include "stdio.h"
#include "string.h"
#include "i2c.h"
#include "cmsis_os2.h"
#include "main.h"

#define OCTET_BIT_LEN 8
#define UUID_LEN_2 2
uint16_t leida_flag=0;
/* sle pair acb handle */
uint16_t g_sle_pair_hdl;
//uint8_t uart1_recv[18] = {0};

/* 串口接收缓冲区大小 */

#define SLE_MTU_SIZE_DEFAULT 512
#define DEFAULT_PAYLOAD_SIZE (SLE_MTU_SIZE_DEFAULT - 12) // 设置有效载荷，否则客户端接受会有问题
/* Service UUID */
#define SLE_UUID_SERVER_SERVICE 0xABCD
/* Property UUID */
#define SLE_UUID_SERVER_NTF_REPORT 0xBCDE
/* 广播ID */
#define SLE_ADV_HANDLE_DEFAULT 1
/* sle server app uuid for test */
static char g_sle_uuid_app_uuid[UUID_LEN_2] = {0x0, 0x0};
/* server notify property uuid for test */
static char g_sle_property_value[OCTET_BIT_LEN] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
/* sle connect acb handle */
static uint16_t g_sle_conn_hdl = 0;//连接句柄
/* sle server handle */
static uint8_t g_server_id = 0;//服务器实例句柄
/* sle service handle */
static uint16_t g_service_handle = 0;//服务句柄
/* sle ntf property handle */
static uint16_t g_property_handle = 0;//​属性句柄

#define UUID_16BIT_LEN 2
#define UUID_128BIT_LEN 16

static uint8_t g_sle_base[] = {0x73, 0x6C, 0x65, 0x5F, 0x74, 0x65, 0x73, 0x74};

uint16_t get_connect_id(void)
{
    return g_sle_conn_hdl;
}

uint16_t sle_uart_client_is_connected(void)
{
    return g_sle_pair_hdl;
}

/*函数的作用是 ​将一个16位无符号整数（uint16_t）以小端字节序（Little-Endian）编码到指定的内存地址*/
static void encode2byte_little(uint8_t *ptr, uint16_t data)
{
    *(uint8_t *)((ptr) + 1) = (uint8_t)((data) >> 0x8);
    *(uint8_t *)(ptr) = (uint8_t)(data);
}

/*调用 sle_uuid_set_base(out) 生成基础UUID模板*/
static void sle_uuid_set_base(sle_uuid_t *out)
{
    errcode_t ret;
    ret = memcpy_s(out->uuid, SLE_UUID_LEN, g_sle_base, SLE_UUID_LEN);//安全拷贝全局基础UUID g_sle_base 到 out->uuid，通过sle_uuid_t *out输出型指针结构体变量传递给外部UUID和Len
    if (ret != EOK) {
        printf("[%s] memcpy fail\n", __FUNCTION__);
        out->len = 0;
        return;
    }
    out->len = UUID_LEN_2;
}

/*函数的作用是 ​基于基础UUID模板生成特定类型的16位UUID变体*/
static void sle_uuid_setu2(uint16_t u2, sle_uuid_t *out)
{
    sle_uuid_set_base(out);//通过out去接收，因为同为sle_uuid_t *型，所以不用&符号
    out->len = UUID_LEN_2;
    encode2byte_little(&out->uuid[14], u2); /* 14: index */
}

static void sle_uuid_print(sle_uuid_t *uuid)
{
    printf("[%s] ", __FUNCTION__);
    if (uuid == NULL) {
        printf("uuid is null\r\n");
        return;
    }
    if (uuid->len == UUID_16BIT_LEN) {
        printf("uuid: %02x %02x.\n", uuid->uuid[14], uuid->uuid[15]); /* 14 15: 下标 */
    } else if (uuid->len == UUID_128BIT_LEN) {
        for (size_t i = 0; i < UUID_128BIT_LEN; i++) {
            /* code */
            printf("0x%02x ", uuid->uuid[i]);
        }
    }
}

static void ssaps_mtu_changed_cbk(uint8_t server_id, uint16_t conn_id, ssap_exchange_info_t *mtu_size, errcode_t status)
{
    printf("[%s] server_id:%d, conn_id:%d, mtu_size:%d, status:%d\r\n", __FUNCTION__, server_id, conn_id,
           mtu_size->mtu_size, status);

        if (g_sle_pair_hdl == 0) 
        {
            g_sle_pair_hdl = conn_id + 1;
        }
}

/*开启服务回调函数*/
static void ssaps_start_service_cbk(uint8_t server_id, uint16_t handle, errcode_t status)
{
    printf("[%s] server_id:%d, handle:%x, status:%x\r\n", __FUNCTION__, server_id, handle, status);
}
/*添加服务回调函数*/
static void ssaps_add_service_cbk(uint8_t server_id, sle_uuid_t *uuid, uint16_t handle, errcode_t status)
{
    printf("[%s] server_id:%x, handle:%x, status:%x\r\n", __FUNCTION__, server_id, handle, status);
    sle_uuid_print(uuid);
}

/*添加特征回调函数*/
static void ssaps_add_property_cbk(uint8_t server_id,
                                   sle_uuid_t *uuid,
                                   uint16_t service_handle,
                                   uint16_t handle,
                                   errcode_t status)
{
    printf("[%s] server_id:%x, service_handle:%x,handle:%x, status:%x\r\n", __FUNCTION__, server_id, service_handle,
           handle, status);
    sle_uuid_print(uuid);
}
/*添加描述回调函数*/
static void ssaps_add_descriptor_cbk(uint8_t server_id,
                                     sle_uuid_t *uuid,
                                     uint16_t service_handle,
                                     uint16_t property_handle,
                                     errcode_t status)
{
    printf("[%s] server_id:%x, service_handle:%x, property_handle:%x,status:%x\r\n", __FUNCTION__, server_id,
           service_handle, property_handle, status);
    sle_uuid_print(uuid);
}

/*删除所有服务回调函数*/
static void ssaps_delete_all_service_cbk(uint8_t server_id, errcode_t status)
{
    printf("[%s] server_id:%x, status:%x\r\n", __FUNCTION__, server_id, status);
}

/*触发条件：当客户端（如手机、IoT设备等）向服务端发起读取属性值的操作时触发*/
static void ssaps_read_request_cbk(uint8_t server_id,
                                   uint16_t conn_id,
                                   ssaps_req_read_cb_t *read_cb_para,
                                   errcode_t status)
{
    printf("[%s] server_id:%x, conn_id:%x, handle:%x, status:%x\r\n", __FUNCTION__, server_id, conn_id,
           read_cb_para->handle, status);
}
/*触发条件：当客户端向服务端发起写入属性值的操作时触发。*/
static void ssaps_write_request_cbk(uint8_t server_id,
                                    uint16_t conn_id,
                                    ssaps_req_write_cb_t *write_cb_para,
                                    errcode_t status)
{
    unused(status);
    printf("[%s] server_id:%x, conn_id:%x, handle:%x\r\n", __FUNCTION__, server_id, conn_id, write_cb_para->handle);
    if ((write_cb_para->length > 0) && write_cb_para->value) {//当客户端向服务端写入数据时 ssaps_write_request_cbk 被调用，写入数据缓存在 write_cb_para->value 中
        printf("recv len:%d data: ", write_cb_para->length);
        for (uint16_t i = 0; i < write_cb_para->length; i++) {
            printf("%c", write_cb_para->value[i]);
        }
        printf("\r\n");
        
        uint8_t *recv_data = write_cb_para->value;
        uint16_t data_len = write_cb_para->length;
        
        // 转换为字符串（需确保数据以'\0'结尾）
        char str_buf[data_len + 1];//动态缓冲区大小 data_len + 1 防止溢出
        memcpy(str_buf, recv_data, data_len);//memcpy 将原始二进制数据复制到本地缓冲区 str_buf
        str_buf[data_len] = '\0'; // 添加终止符

        // 条件判断
        if(strncmp(str_buf, "Turn Right", data_len) == 0){
            leida_flag=1;
            
            printf("right\n");
        }


        if(strncmp(str_buf, "Turn Left", data_len) == 0){
            leida_flag=2;
        
           printf("left\n");
        }
        
     /*   if (strncmp(str_buf, "Turn Right", data_len) == 0) {
            // 执行匹配操作，例如显示到OLED
            ssd1306_show_str(5, 0, "   Turn Right  ", TEXT_SIZE_16);
            
        }
        if (strncmp(str_buf, "Turn Left", data_len) == 0) {
            // 执行匹配操作，例如显示到OLED
            ssd1306_show_str(5, 0, "   Turn Left  ", TEXT_SIZE_16);
        }
        if (strncmp(str_buf, "Call Father", data_len) == 0) {
            // 执行匹配操作，例如显示到OLED
            ssd1306_show_str(5, 0, "   Call Father  ", TEXT_SIZE_16);
        }
        if (strncmp(str_buf, "Call Mother", data_len) == 0) {
            // 执行匹配操作，例如显示到OLED
            ssd1306_show_str(5, 0, "   Call Mother  ", TEXT_SIZE_16);
        }
        if (strncmp(str_buf, "Call Person", data_len) == 0) {
            // 执行匹配操作，例如显示到OLED
            ssd1306_show_str(5, 0, "   Call Person  ", TEXT_SIZE_16);
        }*/
    }
}

/*注册SSAP（SLE Service Access Protocol）服务端的回调函数*/
static errcode_t sle_ssaps_register_cbks(void)
{
    errcode_t ret;
    ssaps_callbacks_t ssaps_cbk = {0};
    ssaps_cbk.add_service_cb = ssaps_add_service_cbk;
    ssaps_cbk.add_property_cb = ssaps_add_property_cbk;
    ssaps_cbk.add_descriptor_cb = ssaps_add_descriptor_cbk;
    ssaps_cbk.start_service_cb = ssaps_start_service_cbk;
    ssaps_cbk.delete_all_service_cb = ssaps_delete_all_service_cbk;
    ssaps_cbk.mtu_changed_cb = ssaps_mtu_changed_cbk;
    ssaps_cbk.read_request_cb = ssaps_read_request_cbk;
    ssaps_cbk.write_request_cb = ssaps_write_request_cbk;
    ret = ssaps_register_callbacks(&ssaps_cbk);
    if (ret != ERRCODE_SLE_SUCCESS) {
        printf("[%s] fail :%x\r\n", __FUNCTION__, ret);
        return ret;
    }
    return ERRCODE_SLE_SUCCESS;
}

/*添加服务的UUID*/
/*添加服务UUID完成后触发ssaps_add_service_cbk回调*/
static errcode_t sle_uuid_server_service_add(void)
{
    errcode_t ret;
    sle_uuid_t service_uuid = {0};
    sle_uuid_setu2(SLE_UUID_SERVER_SERVICE, &service_uuid);//调用 sle_uuid_setu2 时，首先通过 sle_uuid_set_base 将全局预设的128位基础UUID模板（如网页6中 g_sle_base 数组的 0x37BE...0000）复制到 service_uuid.uuid 字段。
    ret = ssaps_add_service_sync(g_server_id, &service_uuid, 1, &g_service_handle);//调用 ssaps_add_service_sync 将服务 UUID 绑定到服务器实例,同时生成句柄，将其存储在全局变量 g_service_handle中。以便后续调用
    if (ret != ERRCODE_SLE_SUCCESS) {
        printf("[%s] fail, ret:%x\r\n", __FUNCTION__, ret);
        return ERRCODE_SLE_FAIL;
    }
    return ERRCODE_SLE_SUCCESS;
}

/*属性(特征)注册*/
/*完成后触发ssaps_add_property_cbk回调*/
static errcode_t sle_uuid_server_property_add(void)
{
    errcode_t ret;
    ssaps_property_info_t property = {0};
    ssaps_desc_info_t descriptor = {0};
    uint8_t ntf_value[] = {0x01, 0x0};

    property.permissions = SSAP_PERMISSION_READ | SSAP_PERMISSION_WRITE;
    property.operate_indication = SSAP_OPERATE_INDICATION_BIT_READ | SSAP_OPERATE_INDICATION_BIT_NOTIFY;
    sle_uuid_setu2(SLE_UUID_SERVER_NTF_REPORT, &property.uuid);
    property.value = (uint8_t *)osal_vmalloc(sizeof(g_sle_property_value));
    if (property.value == NULL) {
        printf("[%s] property mem fail.\r\n", __FUNCTION__);
        return ERRCODE_SLE_FAIL;
    }
    if (memcpy_s(property.value, sizeof(g_sle_property_value), g_sle_property_value, sizeof(g_sle_property_value)) !=
        EOK) {
        printf("[%s] property mem cpy fail.\r\n", __FUNCTION__);
        osal_vfree(property.value);
        return ERRCODE_SLE_FAIL;
    }
    ret = ssaps_add_property_sync(g_server_id, g_service_handle, &property, &g_property_handle);//使用 ssaps_add_property_sync 接口注册属性时，协议栈会分配一个唯一句柄并返回，同时触发ssaps_add_property_cbk回调
    if (ret != ERRCODE_SLE_SUCCESS) {
        printf("[%s] fail, ret:%x\r\n", __FUNCTION__, ret);
        osal_vfree(property.value);
        return ERRCODE_SLE_FAIL;
    }
    descriptor.permissions = SSAP_PERMISSION_READ | SSAP_PERMISSION_WRITE;
    descriptor.type = SSAP_DESCRIPTOR_USER_DESCRIPTION;
    descriptor.operate_indication = SSAP_OPERATE_INDICATION_BIT_READ | SSAP_OPERATE_INDICATION_BIT_WRITE;
    descriptor.value = ntf_value;
    descriptor.value_len = sizeof(ntf_value);

    ret = ssaps_add_descriptor_sync(g_server_id, g_service_handle, g_property_handle, &descriptor);
    if (ret != ERRCODE_SLE_SUCCESS) {
        printf("[%s] fail, ret:%x\r\n", __FUNCTION__, ret);
        osal_vfree(property.value);
        osal_vfree(descriptor.value);
        return ERRCODE_SLE_FAIL;
    }
    osal_vfree(property.value);
    return ERRCODE_SLE_SUCCESS;
}
/*添加服务回调函数*/
/*sle_uuid_server_service_add完成后触发ssaps_add_service_cbk回调*/
/*ssaps_start_service完成后触发ssaps_start_service_cbk回调*/
static errcode_t sle_server_add(void)
{
    errcode_t ret;
    sle_uuid_t app_uuid = {0};

    printf("[%s] in\r\n", __FUNCTION__);
    app_uuid.len = sizeof(g_sle_uuid_app_uuid);
    if (memcpy_s(app_uuid.uuid, app_uuid.len, g_sle_uuid_app_uuid, sizeof(g_sle_uuid_app_uuid)) != EOK) {
        return ERRCODE_SLE_FAIL;
    }
    ssaps_register_server(&app_uuid, &g_server_id);//注册SSAP的服务端,同时生成唯一的g_server_id

    if (sle_uuid_server_service_add() != ERRCODE_SLE_SUCCESS) {//注释：sle_uuid_server_service_add()是添加服务的UUID，完成后触发ssaps_add_service_cbk回调
        ssaps_unregister_server(g_server_id);
        return ERRCODE_SLE_FAIL;
    }
    if (sle_uuid_server_property_add() != ERRCODE_SLE_SUCCESS) {//注释：sle_uuid_server_property_add()是添加特征UUID
        ssaps_unregister_server(g_server_id);
        return ERRCODE_SLE_FAIL;
    }
    printf("[%s] server_id:%x, service_handle:%x, property_handle:%x\r\n", __FUNCTION__, g_server_id, g_service_handle,
           g_property_handle);
    ret = ssaps_start_service(g_server_id, g_service_handle);//注释：ssaps_start_service开启服务，触发ssaps_start_service_cbk回调
    if (ret != ERRCODE_SLE_SUCCESS) {
        printf("[%s] fail, ret:%x\r\n", __FUNCTION__, ret);
        return ERRCODE_SLE_FAIL;
    }
    printf("[%s] out\r\n", __FUNCTION__);
    return ERRCODE_SLE_SUCCESS;
}

/* device通过uuid向host发送数据：report */
errcode_t sle_server_send_report_by_uuid(msg_data_t msg_data)
{
    ssaps_ntf_ind_by_uuid_t param = {0};
    param.type = SSAP_PROPERTY_TYPE_VALUE;
    param.start_handle = g_service_handle;
    param.end_handle = g_property_handle;
    param.value_len = msg_data.value_len;
    param.value = msg_data.value;
    sle_uuid_setu2(SLE_UUID_SERVER_NTF_REPORT, &param.uuid);
    ssaps_notify_indicate_by_uuid(g_server_id, g_sle_conn_hdl, &param);
    return ERRCODE_SLE_SUCCESS;
}

/* device通过handle向host发送数据：report */
errcode_t sle_server_send_report_by_handle(msg_data_t msg_data)
{
    ssaps_ntf_ind_t param = {0};
    param.handle = g_property_handle;
    param.type = SSAP_PROPERTY_TYPE_VALUE;
    param.value = msg_data.value;
    param.value_len = msg_data.value_len;
    return ssaps_notify_indicate(g_server_id, g_sle_conn_hdl, &param);
}

/*客户端连接时触发sle_connect_state_changed_cbk，记录g_sle_conn_hdl连接句柄。
断开连接时重置句柄并重启广播。*/
static void sle_connect_state_changed_cbk(uint16_t conn_id,
                                          const sle_addr_t *addr,
                                          sle_acb_state_t conn_state,
                                          sle_pair_state_t pair_state,
                                          sle_disc_reason_t disc_reason)
{
    printf("[%s] conn_id:0x%02x, conn_state:0x%x, pair_state:0x%x, disc_reason:0x%x\r\n", __FUNCTION__, conn_id,
           conn_state, pair_state, disc_reason);
    printf("[%s] addr:%02x:**:**:**:%02x:%02x\r\n", __FUNCTION__, addr->addr[0], addr->addr[4]); /* 0 4: index */
    if (conn_state == SLE_ACB_STATE_CONNECTED) {
        g_sle_conn_hdl = conn_id;//记录当前活跃连接的句柄,此赋值操作仅在 ​连接成功建立​（conn_state == SLE_ACB_STATE_CONNECTED）时执行，确保只有有效连接才会更新全局句柄。
        sle_set_data_len(conn_id, DEFAULT_PAYLOAD_SIZE); // 设置有效载荷
    } else if (conn_state == SLE_ACB_STATE_DISCONNECTED) {
        g_sle_conn_hdl = 0;//当连接断开（conn_state == SLE_ACB_STATE_DISCONNECTED）时，将 g_sle_conn_hdl 重置为 0，并重新启动广播（sle_start_announce），使设备可被其他客户端发现。
        g_sle_pair_hdl = 0;
        sle_start_announce(SLE_ADV_HANDLE_DEFAULT);
    }
}

/*该回调函数属于星闪（SLE）协议栈中客户端侧的配对完成事件处理接口。当客户端与服务端完成配对流程（包括密钥交换、权限协商等）后，协议栈会调用此函数通知应用层配对结果*/
static void sle_pair_complete_cbk(uint16_t conn_id, const sle_addr_t *addr, errcode_t status)
{
    printf("[%s] conn_id:%02x, status:%x\r\n", __FUNCTION__, conn_id, status);
    printf("[%s] addr:%02x:**:**:**:%02x:%02x\r\n", __FUNCTION__, addr->addr[0], addr->addr[4]); /* 0 4: index */
    g_sle_pair_hdl = conn_id + 1;
    ssap_exchange_info_t parameter = { 0 };
    parameter.mtu_size = 520;
    parameter.version = 1;
    ssaps_set_info(g_server_id, &parameter);
}

/*注册设备连接回调函数*/
static errcode_t sle_conn_register_cbks(void)
{
    errcode_t ret;
    sle_connection_callbacks_t conn_cbks = {0};
    conn_cbks.connect_state_changed_cb = sle_connect_state_changed_cbk;
    conn_cbks.pair_complete_cb = sle_pair_complete_cbk;
    ret = sle_connection_register_callbacks(&conn_cbks);
    if (ret != ERRCODE_SLE_SUCCESS) {
        printf("[%s] fail :%x\r\n", __FUNCTION__, ret);
        return ret;
    }
    return ERRCODE_SLE_SUCCESS;
}

/* 初始化uuid server */
errcode_t sle_server_init(void)
{
    errcode_t ret;

    /* 使能SLE */
    if (enable_sle() != ERRCODE_SUCC) {
        printf("sle enbale fail !\r\n");
        return ERRCODE_FAIL;
    }

    ret = sle_announce_register_cbks();
    if (ret != ERRCODE_SLE_SUCCESS) {
        printf("[%s] sle_announce_register_cbks fail :%x\r\n", __FUNCTION__, ret);
        return ret;
    }
    ret = sle_conn_register_cbks();
    if (ret != ERRCODE_SLE_SUCCESS) {
        printf("[%s] sle_conn_register_cbks fail :%x\r\n", __FUNCTION__, ret);
        return ret;
    }
    ret = sle_ssaps_register_cbks();
    if (ret != ERRCODE_SLE_SUCCESS) {
        printf("[%s] sle_ssaps_register_cbks fail :%x\r\n", __FUNCTION__, ret);
        return ret;
    }
    ret = sle_server_add();
    if (ret != ERRCODE_SLE_SUCCESS) {
        printf("[%s] sle_server_add fail :%x\r\n", __FUNCTION__, ret);
        return ret;
    }
    
    
    ret = sle_server_adv_init();//设置广播数据
    if (ret != ERRCODE_SLE_SUCCESS) {
        printf("[%s] sle_server_adv_init fail :%x\r\n", __FUNCTION__, ret);
        return ret;
    }
    printf("[%s] init ok\r\n", __FUNCTION__);
    return ERRCODE_SLE_SUCCESS;
}
/*
static void *sle_server_task(const char *arg)
{
    unused(arg);
    osal_msleep(500); 500: 延时500ms 
    sle_server_init();
    uart_gpio_init();
    uart_init_config();
   // ssd1306_init(); // OLED 显示屏初始化
  //  ssd1306_cls();  // 清屏
    //ssd1306_show_str(5, 0, "   Call Father  ", TEXT_SIZE_16);
    
    while (1) {
        msg_data_t msg_data = {0};
        int msg_ret = osal_msg_queue_read_copy(g_msg_queue, &msg_data, &g_msg_rev_size, OSAL_WAIT_FOREVER);
        if (msg_ret != OSAL_SUCCESS) {
            printf("msg queue read copy fail.");
            if (msg_data.value != NULL) {
                osal_vfree(msg_data.value);
            }
        }
        if (msg_data.value != NULL) {
            sle_server_send_report_by_handle(msg_data);
            
        }
    }
    return NULL;
}
static void sle_server_entry(void)
{
    osal_task *task_handle = NULL;
    osal_kthread_lock();
    int ret = osal_msg_queue_create("sle_msg", g_msg_rev_size, &g_msg_queue, 0, g_msg_rev_size);
    if (ret != OSAL_SUCCESS) {
        printf("create queue failure!,error:%x\n", ret);
    }

    task_handle =osal_kthread_create((osal_kthread_handler)sle_server_task, 0, "sle_server_task", SLE_SERVER_STACK_SIZE);
        
    if (task_handle != NULL) {
        osal_kthread_set_priority(task_handle, SLE_SERVER_TASK_PRIO);
        osal_kfree(task_handle);
    }
    osal_kthread_unlock();
}
*/
/* Run the sle_entry. */
//app_run(sle_server_entry);

/* 串口接收回调 */
/*void sle_server_read_handler(const void *buffer, uint16_t length, bool error)
{
    unused(error);

    msg_data_t msg_data = {0};
    void *buffer_cpy = osal_vmalloc(length);
    if (memcpy_s(buffer_cpy, length, buffer, length) != EOK) {
        osal_vfree(buffer_cpy);
        return;
    }
    msg_data.value = (uint8_t *)buffer_cpy;
    msg_data.value_len = length;
    osal_msg_queue_write_copy(g_msg_queue, (void *)&msg_data, g_msg_rev_size, 0);
}*/

/*
void uart2_read_handler(const void *buffer, uint16_t length, bool error)
{
    unused(error);
    if (buffer == NULL || length == 0) {
        return;
    }

    // 动态分配内存
    uint8_t *buffer_cpy = osal_vmalloc(length);
    if (buffer_cpy == NULL) {
        printf("Failed to allocate memory for buffer_cpy");
        return;
    }
    // 复制数据
    if (memcpy_s(buffer_cpy, length, buffer, length) != EOK) {
        osal_vfree(buffer_cpy);
        return;
    }

    // 打印数据
    for (int i = 0; i < length; i++) {
        printf("%02X ", buffer_cpy[i]);
    }
    printf("\r");

    // 发送到消息队列
    msg_data_t msg_data = {0};
    msg_data.value = buffer_cpy;  // 指向动态内存
    msg_data.value_len = length;
    osal_msg_queue_write_copy(g_msg_queue, &msg_data, sizeof(msg_data), 0);
}
*/
